package com.zhuimeng.mysql_rocketmq.listener;

import com.zhuimeng.mysql_rocketmq.dao.SkillOrderRepository;
import com.zhuimeng.mysql_rocketmq.pojo.SkillOrder;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
@RocketMQMessageListener(topic = "DELETEORDER",consumerGroup = "DELETEORDER_CONSUMER")
public class DelayedMessageListener implements RocketMQListener<String> {
    @Autowired
    SkillOrderRepository skillOrderRepository;
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    @Override
    public void onMessage(String userId) {
        SkillOrder skillOrderWhere = new SkillOrder();
        skillOrderWhere.setUserId(userId);
        skillOrderWhere.setOrderStatus("未支付");
        final List<SkillOrder> skillOrders = skillOrderRepository.findAll(Example.of(skillOrderWhere));
        if (skillOrders != null && skillOrders.size()>0) {
            skillOrderRepository.deleteById(skillOrders.get(0).getOrderId());
            rocketMQTemplate.convertAndSend("REDISINCR",userId);
        }
    }
}
